import java.util.Arrays;
import java.util.Comparator;

/**
 * @Author YuanYuan
 * @Date 2022/5/19
 * @Time 14:14
 */

class Student implements Comparable<Student>{
    public String name;
    public int age;
    public double score;

    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }
        @Override
    public int compareTo(Student o) {
        return this.age-o.age;
    }
}
class AgeCompare implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }
}
class ScoreCompare implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return (int)(o1.score - o2.score);
    }

}

class NameCompare implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}

public class TestDemo {

    public static void sort(Comparable[] comparables) {
        boolean flag = false;
        for (int i = 0; i < comparables.length-1; i++) {
            flag = false;
            for (int j = 0; j < comparables.length-i-1; j++) {
                if(comparables[j].compareTo(comparables[j+1]) > 0) {
                    Comparable temp = comparables[j];
                    comparables[j] = comparables[j+1];
                    comparables[j+1] = temp;
                    flag = true;
                }
            }
            if(!flag){
                return;
            }
        }
    }

    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("yuanyuan",18,99.7);
        students[1]  = new Student("xianxian",21,99.8);
        students[2]  = new Student("ruirui",20,100);

        sort(students);
        System.out.println(Arrays.toString(students));
    }
    public static void main3(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("yuanyuan",18,99.7);
        students[1]  = new Student("xianxian",19,99.8);
        students[2]  = new Student("ruirui",20,100);

        Arrays.sort(students,new NameCompare());
        System.out.println(Arrays.toString(students));
    }
    public static void main2(String[] args) {
        Student student1 = new Student("yuanyuan",18,99.7);
        Student student2 = new Student("xianxian",19,99.8);
        Student student3 = new Student("ruirui",20,100);

        NameCompare nameCompare = new NameCompare();
        int ret1 = nameCompare.compare(student1,student2);
        int ret2 = new AgeCompare().compare(student1,student2);
        int ret3 = new ScoreCompare().compare(student1,student2);
        System.out.println(ret1+" " + ret2+" "+ret3);

    }
    public static void main1(String[] args) {
        Student student1 = new Student("yuanyuan",18,99.7);
        Student student2 = new Student("xianxian",19,99.8);
        Student student3 = new Student("ruirui",20,100);
//        if(student1.compareTo(student2)>0){
//            System.out.println(student1.name+"大");
//        }else {
//            System.out.println(student1.name+"小");
//        }

    }
}
